import 'dart:collection';
import 'dart:convert' show json;

import '../emoj_test.dart';

class WorldCityInfo {

  Location location;

  WorldCityInfo.fromParams({this.location});
  WorldCityInfo.empty();
  factory WorldCityInfo(jsonStr) => jsonStr == null ? null : jsonStr is String ? new WorldCityInfo.fromJson(json.decode(jsonStr)) : new WorldCityInfo.fromJson(jsonStr);

  WorldCityInfo.fromJson(jsonRes) {
    location = jsonRes['Location'] == null ? null : new Location.fromJson(jsonRes['Location']);
  }

  @override
  String toString() {
    return '{"Location": $Location}';
  }
}

class Location {

  List<CountryRegion> countryRegion;

  Location.fromParams({this.countryRegion});

  Location.fromJson(jsonRes) {
    countryRegion = jsonRes['CountryRegion'] == null ? null : [];

    for (var CountryRegionItem in CountryRegion == null ? [] : jsonRes['CountryRegion']){
      countryRegion.add(CountryRegionItem == null ? null : new CountryRegion.fromJson(CountryRegionItem));
    }
  }

  @override
  String toString() {
    return '{"CountryRegion": $CountryRegion}';
  }
}

class CountryRegion {

  String code;
  String englishName = '';
  String chineseName = '';
  String koreaName = '';
  String janName = '';
  String traditionalName = '';



  List<State> states = [];

  CountryRegion.fromParams({this.code, this.englishName, this.states});

  CountryRegion.fromJson(jsonRes) {
    code = jsonRes['Code'] ?? '';
    englishName = jsonRes['Name'] ?? '';
    if(jsonRes['State'] is String){
      var state = jsonRes['State'] == null ? null : new State.fromJson(jsonRes['State']);
      states.add(state);
    }else if(jsonRes['State']  is Map){
      for(int i = 0;i < jsonRes['State'].length;i++){
        states.add((State.fromJson(jsonRes['State']['City'])));
      }
    }else{//is list
//      states.add(State.fromParams())
    }
  }

  @override
  String toString() {
    return '{"Code": ${code != null?'${json.encode(code)}':'null'},"Name": ${englishName != null?'${json.encode(englishName)}':'null'},"State": $State}';
  }
}

class State {
  var code;
  String name;
  List<City> city = [];

  State.fromParams({this.city,this.code});

  State.fromJson(jsonRes) {
    for(int i = 0;i < jsonRes.length;i++){
      city.add(City.fromParams(code:jsonRes[i]['Code'], englishName:jsonRes[i]['Name']));
    }
    code = "${jsonRes['Code']}" ?? '';
    name = "${jsonRes['Name']}" ?? '';

    if(!isEmptyString(code) && !isEmptyString(name)){
//      print('不为空');
    }

//    print(jsonRes.runtimeType.toString());
    if(!(jsonRes is List<dynamic>)){
      var a = jsonRes['Code'].runtimeType;
//      print("==================>$a");
    }else{
//      print('------------------');
    }
  }

  @override
  String toString() {
    return '{"City": $City}';
  }
}

class City {

  String code;
  String englishName = '';
  String chineseName = '';
  String koreaName = '';
  String janName = '';
  String traditionalName = '';

  City.fromParams({this.code, this.englishName});

  City.fromJson(jsonRes) {
    code = jsonRes['Code'];
    englishName = jsonRes['Name'];
  }

  @override
  String toString() {
    return '{"Code": ${code != null?'${json.encode(code)}':'null'},"Name": ${englishName != null?'${json.encode(englishName)}':'null'}}';
  }
}

